#include "speechManager.h"
#include "speaker.h"
using namespace std;


SpeechManager::SpeechManager()
{
    this->initSpeech();

    this->creatSpeaker();
}

SpeechManager::~SpeechManager()
{
}

void SpeechManager::show_menu()
{
    cout << "***************************************" << endl;
    cout << "*********  欢迎参加演讲比赛  **********" << endl;
    cout << "*********   1.开始演讲比赛   **********" << endl;
    cout << "*********   2.查看往届记录   **********" << endl;
    cout << "*********   3.清空比赛记录   **********" << endl;
    cout << "*********   0.退出比赛程序   **********" << endl;
    cout << "***************************************" << endl;
    cout << endl;
}

void SpeechManager::exitSystem()
{
    cout << "期待下次使用,再见" << endl;
    exit(0);
}

void SpeechManager::initSpeech()
{
    //将容器内部置空
    this->v1.clear();
    this->v2.clear();
    this->vVictory.clear();
    this->m_Speaker.clear();
    this->m_Record.clear();

    //将比赛轮数设为１
    this->m_Index = 1;
}

void SpeechManager::creatSpeaker()
{
    //选手名称为ABCDEFGHIJKLd
    string nameSeed = "ABCDEFGHIJKL";
    for (int i = 0; i < nameSeed.size(); i++)
    {
        string name = "选手";
        name += nameSeed[i];

        //创建具体的选手
        Speaker sp;

        //初始化选手属性
        // sp.m_Name = name;
        // for (int j = 0; j < 2; j++)
        // {
        //     sp.m_Score[j] = 0;
        // }

        // 将选手编号放入v1中
        this->v1.push_back(i + 1001);

        //将选手编号和姓名放入map容器中
        this->m_Speaker.insert(make_pair(i + 10001, sp));
        this->m_Speaker[1001 + i].m_Name = name;
        for (int j = 0; j < 2; j++)
        {
            this->m_Speaker[1001 + i].m_Score[j] = 0;
        }
    }
}

void SpeechManager::startSpeech()
{
    this->speechDraw();

    this->speechContest();

    this->showScore();

    this->m_Index++;
}

void SpeechManager::speechDraw()
{
    cout << "第＞＞" << this->m_Index << "＜＜轮选手正在抽签" << endl;
    cout << "-------------------------------" << endl;
    cout << "抽签结果如下：" << endl;

    if (this->m_Index == 1)
    {
        //第一轮比赛抽签
        random_shuffle(v1.begin(), v1.end());

        for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
        {
            cout << *it << "  ";
        }
        cout << endl;
    }
    else
    {
        //第二轮比赛抽签
        random_shuffle(v2.begin(), v2.end());

        for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
        {
            cout << *it << "  ";
        }
        cout << endl;
    }
    cout << "-------------------------------" << endl;
    cout << endl;
}

void SpeechManager::speechContest()
{
    cout << "第＞＞" << this->m_Index << "＜＜轮比赛正式开始" << endl;

    //准备临时容器存储比赛中的　分数＼选手编号，并按分数进行从大到小排列　　
    multimap<double, int, greater<double> > groupScore;

    int num = 0; //记录人员个数，一组６人

    vector<int> v_Src; //比赛选手容器
    if (this->m_Index == 1)
    {
        v_Src = v1;
    }
    else
    {
        v_Src = v2;
    }

    //遍历所有选手进行比赛
    for (vector<int>::iterator it = v_Src.begin(); it != v_Src.end(); it++)
    {
        num++;
        //定义评委打分容器
        deque<double> d;

        for (int i = 0; i < 10; i++)
        {
            double score = (rand() % 401 + 600) / 10.f;
            // cout << score << endl;

            //将十名评委给的成绩放入容器中
            d.push_back(score);
        }
        //对分数进行排序
        sort(d.begin(), d.end(), greater<double>());

        //去除最高分
        d.pop_front();
        //去除最低分
        d.pop_back();
        //计算平均分
        double sum = accumulate(d.begin(), d.end(), 0.0f);
        double avg = sum / (double)d.size();

        // 将平均分放入map容器中
        this->m_Speaker[*it].m_Score[this->m_Index - 1] = avg;

        //将分数信息放入到临时小组容器中
        groupScore.insert(make_pair(avg, *it));

        //分别取出每６人的前三名
        if (num % 6 == 0)
        {
            cout << "第" << num / 6 << "小组比赛名次" << endl;
            for (multimap<double, int, greater<double> >::iterator it = groupScore.begin(); it != groupScore.end(); it++)
            {
                cout << "编号：" << it->second << " "
                     << "姓名：" << this->m_Speaker[it->second].m_Name
                     << " "
                     << "成绩：" << this->m_Speaker[it->second].m_Score[m_Index - 1] << endl;
            }
            // 取走前三名,放入v2中
            int count = 0;
            for (multimap<double, int, greater<double> >::iterator it = groupScore.begin(); it != groupScore.end() & count < 3; it++, count++)
            {
                if (this->m_Index == 1)
                {
                    v2.push_back((*it).second); //将每组的前三名放入v2容器中
                }
                else
                {
                    vVictory.push_back((*it).second);
                }
            }
            groupScore.clear(); //将小组容器清空
            cout << endl;
        }
    }
    cout << "----------第《 " << this->m_Index << " 》轮比赛完毕----------" << endl;
    cout << endl;
}

void SpeechManager::showScore()
{
    cout << "----------第《 " << this->m_Index << " 》轮晋级选手信息如下----------" << endl;
    vector<int> v3;
    if (this->m_Index == 1)
    {
        v3 = v2;
    }
    else
    {
        v3 = vVictory;
    }
    for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++)
    {
        // cout <<this->m_Speaker[*it]
        cout << "选手编号：" << *it << " 姓名：" << this->m_Speaker[*it].m_Name
             << " 得分：" << this->m_Speaker[*it].m_Score[this->m_Index - 1] << endl;
    }
    cout << endl;
}

void SpeechManager::saveRecord()
{
    //定义输出文件流，
    ofstream ofs;
    //以追加的方式创建文件
    ofs.open("/home/wdd/Desktop/c++_ws/基于STL的演讲比赛流程管理系统/log/speech.csv", ios::out | ios::app);

    //将获胜选手的数据写入文件
    for (vector<int>::iterator it = vVictory.begin(); it != vVictory.end(); it++)
    {
        ofs << "选手编号：" << *it << " 姓名：" << this->m_Speaker[*it].m_Name
            << " 得分：" << this->m_Speaker[*it].m_Score[1] << endl;
    }
    ofs << endl;
    cout << "记录保存完毕" << endl;
    ofs.close();

    //文件为空标志置为假
    this->fileIsEmpty = false;
}

void SpeechManager::loadRecord()
{
    ifstream ifs;
    ifs.open("/home/wdd/Desktop/c++_ws/基于STL的演讲比赛流程管理系统/log/speech.csv", ios::in);
    if (!ifs.is_open())
    {
        cout << "文件打开失败" << endl;
        this->fileIsEmpty = true;
        ifs.close();
        return;
    }

    char ch;
    ifs >> ch;
    if (ifs.eof())
    {
        cout << "文件为空" << endl;
        this->fileIsEmpty = true;
        return;
    }

    this->fileIsEmpty = false;
    //将上面读取的字符放回
    ifs.putback(ch);

    char buffer[256];
    int i = 0;
    int num=0;
    cout << "想要查询第几届比赛的结果(输入０代表全部结果)：";
    cin >> num;
    while (!ifs.eof())
    {
        //按行读取文件，每３行为一届，且还有一空行
        if (i % 4 == 0 )
        {
            //检查当前行是否为文件结尾
            ifs >> ch;
            if (ifs.eof())
            {
                ifs.putback(ch);
                break;
            }
            ifs.putback(ch);
            //如果想要输出全部结果
            if(num==0)
                cout << "-------------"<<"这是第" << i/4+1 <<"届比赛的前三名" << "-------------"<< endl;
            
            //如果想要输出指定结果
            if(i/4+1 == num)
            {
                cout << "-------------"<<"这是第" << i/4+1 <<"届比赛的前三名" << "-------------"<< endl;
                for (int i = 0; i < 3; i++)
                {
                    ifs.getline(buffer, 100);
                    cout << buffer << endl;
                }
                break;
            }
        }
        
        ifs.getline(buffer, 100);
        if(num==0 )
            cout << buffer << endl;
        i++;
    }
    //重置文件指针
    ifs.seekg(0, ios::beg);
}

//清空文件
void SpeechManager::clearRecord()
{
	cout << "是否确定清空文件? " << endl;
	cout << " 1.是        2.否 " << endl;
	int select = 0;
	cin >> select;
	if (select == 1)
	{
		//确认清空
        //ios::trunc  覆盖存在的文件，即如果原来文件中有数据就被清空
		ofstream ofs("/home/wdd/Desktop/c++_ws/基于STL的演讲比赛流程管理系统/log/speech.csv", ios::trunc);
		ofs.close();

		//初始化容器和属性
		this->initSpeech();

		//创建12名选手
		this->creatSpeaker();

		cout << "清空成功！" << endl;
	}

}
